package com.beiing.tianshuai.tianshuai.home;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.beiing.tianshuai.tianshuai.entity.IndexSearchListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jinda Yang on 2017/12/11.
 * Description : 创建搜索表的链接
 */

public class RecordsDao {

    private RecordSQLiteOpenHelper recordHelper;

    private SQLiteDatabase db;

    public RecordsDao(Context context) {
        recordHelper = new RecordSQLiteOpenHelper(context);
    }

    //添加搜索记录
    public void addRecords(IndexSearchListBean.DataBean.ResultBean searchListBean) {
        if (!isHasRecord(searchListBean.getTitle())) {
            db = recordHelper.getReadableDatabase();
            ContentValues values = new ContentValues();
            values.put("title", searchListBean.getTitle());
            values.put("id", searchListBean.getId());
            values.put("type", searchListBean.getIsNum());
            values.put("cover", "");
            values.put("tName", searchListBean.getIsType());
            //添加
            db.insert("search_history", null, values);
            //关闭
            db.close();
        }
    }

    //判断是否含有该搜索记录
    private boolean isHasRecord(String record) {
        boolean isHasRecord = false;
        db = recordHelper.getReadableDatabase();
        Cursor cursor = db.query("search_history", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            if (record.equals(cursor.getString(cursor.getColumnIndexOrThrow("title")))) {
                isHasRecord = true;
            }
        }
        //关闭数据库
        db.close();
        cursor.close();
        return isHasRecord;
    }

    //获取全部搜索记录
    public List<IndexSearchListBean.DataBean.ResultBean> getRecordsList() {
        List<IndexSearchListBean.DataBean.ResultBean> recordsList = new ArrayList<>();
        db = recordHelper.getReadableDatabase();
        Cursor cursor = db.query("search_history", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            IndexSearchListBean.DataBean.ResultBean searchListBean = new IndexSearchListBean.DataBean.ResultBean();
            searchListBean.setTitle(cursor.getString(cursor.getColumnIndexOrThrow("title")));
            searchListBean.setId(cursor.getString(cursor.getColumnIndexOrThrow("id")));
            searchListBean.setIsNum(cursor.getString(cursor.getColumnIndexOrThrow("type")));
            searchListBean.setIsType(cursor.getString(cursor.getColumnIndexOrThrow("tName")));
            recordsList.add(searchListBean);
        }
        //关闭数据库
        db.close();
        cursor.close();
        return recordsList;
    }

    //模糊查询
    public List<String> querySimlarRecord(String record){
        String queryStr = "select * from records where name like '%" + record + "%' order by name ";
        List<String> similarRecords = new ArrayList<>();
        Cursor cursor= recordHelper.getReadableDatabase().rawQuery(queryStr,null);

        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
            similarRecords.add(name);
        }

        cursor.close();
        return similarRecords;
    }

    public void deleteRecord(String delete_id){
        db = recordHelper.getWritableDatabase();
        db.execSQL("delete from search_history where id = " +delete_id);
        db.close();
    }

    //清空搜索记录
    public void deleteAllRecords() {
        db = recordHelper.getWritableDatabase();
        db.execSQL("delete from search_history");
        db.close();
    }
}
